package com.group1.ws;

import java.util.List;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import org.hibernate.Session;

import com.group1.model.Lecturer;
import com.group1.model.Subject;
import com.group1.model.Topic;
import com.group1.util.SessionFactoryUtil;

@Path("/topic")
public class TopicWS {

	@GET
	@Path("/getAll")
	@Produces(MediaType.TEXT_PLAIN)
	public String getAllTopic(){
		
		Session session = SessionFactoryUtil.getSessionFactory().getCurrentSession();
		 session.beginTransaction();
		List<Topic> list = Topic.getAllTopic(session);
		String result;
		result = "{TOPIC:[";
		boolean first = true;
		for (Topic t : list) {
			Integer topicID = t.getTopicId();
			String topicName = t.getTopicName();
			String topicSubject = t.getSubject().subjectName;
			String topicLecturer = t.getLecturer().lecturerId;
			Integer topTopic = t.getTopTopic();
			String topicNote = t.getTopicNote();
			result += (first?"":",");
			first = false;
			result += "{";
			result += "\"topicID\"" + ":\"" + topicID + "\",";
			result += "\"topicName\"" + ":\"" + topicName + "\",";
			result += "\"topicSubject\"" + ":\"" + topicSubject + "\",";
			result += "\"topicLecturer\"" + ":\"" + topicLecturer + "\",";
			result += "\"topTopic\"" + ":\"" + topTopic + "\",";
			result += "\"topicNote\"" + ":\"" + topicNote+ "\"";
			result += "}";
		}
		result += "]}";
		session.getTransaction().commit();
		return result;
	}
	
	@GET
	@Path("/getByTopicID/{topicID}")
	@Produces(MediaType.TEXT_PLAIN)
	public String getByTopicID(@PathParam("topicID") String topicID){
		
		Session session = SessionFactoryUtil.getSessionFactory().getCurrentSession();
		 session.beginTransaction();
		Topic topic = Topic.getTopicByID(Integer.valueOf(topicID), session);
		String result;
		result = "{TOPIC:{";
		result += "\"topicID\"" + ":\"" + topicID + "\",";
		result += "\"topicName\"" + ":\"" + topic.getTopicName() + "\",";
		result += "\"topicSubject\"" + ":\"" + topic.getSubject().subjectName + "\",";
		result += "\"topicLecturer\"" + ":\"" + topic.getLecturer().lecturerId + "\",";
		result += "\"topTopic\"" + ":\"" + topic.getTopTopic() + "\",";
		result += "\"topicNote\"" + ":\"" + topic.getTopicNote()+ "\"";
		result += "}}";
		session.getTransaction().commit();
		return result;
	}
	
	@GET
	@Path("/getBySubjectID/{subjectID}")
	@Produces(MediaType.TEXT_PLAIN)
	public String getBySubjectID(@PathParam("subjectID") String subjectID){
		
		Session session = SessionFactoryUtil.getSessionFactory().getCurrentSession();
		 session.beginTransaction();
		List<Topic> list = Topic.getTopicBySubject(subjectID, session);
		String result;
		result = "{TOPIC:[";
		boolean first = true;
		for (Topic t : list) {
			Integer topicID = t.getTopicId();
			String topicName = t.getTopicName();
			String topicSubject = t.getSubject().subjectName;
			String topicLecturer = t.getLecturer().lecturerId;
			Integer topTopic = t.getTopTopic();
			String topicNote = t.getTopicNote();
			result += (first?"":",");
			first = false;
			result += "{";
			result += "\"topicID\"" + ":\"" + topicID + "\",";
			result += "\"topicName\"" + ":\"" + topicName + "\",";
			result += "\"topicSubject\"" + ":\"" + topicSubject + "\",";
			result += "\"topicLecturer\"" + ":\"" + topicLecturer + "\",";
			result += "\"topTopic\"" + ":\"" + topTopic + "\",";
			result += "\"topicNote\"" + ":\"" + topicNote+ "\"";
			result += "}";
		}
		result += "]}";
		session.getTransaction().commit();
		return result;
	}
	
	@GET
	@Path("/getByTopTopic/{topTopic}")
	@Produces(MediaType.TEXT_PLAIN)
	public String getByTopTopic(@PathParam("topTopic") String topTopicID){
		
		Session session = SessionFactoryUtil.getSessionFactory().getCurrentSession();
		 session.beginTransaction();
		List<Topic> list = Topic.getTopicByTopTopic(Integer.valueOf(topTopicID), session);
		String result;
		result = "{TOPIC:[";
		boolean first = true;
		for (Topic t : list) {
			Integer topicID = t.getTopicId();
			String topicName = t.getTopicName();
			String topicSubject = t.getSubject().subjectName;
			String topicLecturer = t.getLecturer().lecturerId;
			Integer topTopic = t.getTopTopic();
			String topicNote = t.getTopicNote();
			result += (first?"":",");
			first = false;
			result += "{";
			result += "\"topicID\"" + ":\"" + topicID + "\",";
			result += "\"topicName\"" + ":\"" + topicName + "\",";
			result += "\"topicSubject\"" + ":\"" + topicSubject + "\",";
			result += "\"topicLecturer\"" + ":\"" + topicLecturer + "\",";
			result += "\"topTopic\"" + ":\"" + topTopic + "\",";
			result += "\"topicNote\"" + ":\"" + topicNote+ "\"";
			result += "}";
		}
		result += "]}";
		session.getTransaction().commit();
		return result;
	}
	
	@GET
	@Path("/getByLecturerID/{lecturerID}")
	@Produces(MediaType.TEXT_PLAIN)
	public String getByLecturerID(@PathParam("lecturerID") String lecturerID){
		
		Session session = SessionFactoryUtil.getSessionFactory().getCurrentSession();
		 session.beginTransaction();
		List<Topic> list = Topic.getTopicOfLecturer(lecturerID, session);
		String result;
		result = "{TOPIC:[";
		boolean first = true;
		for (Topic t : list) {
			Integer topicID = t.getTopicId();
			String topicName = t.getTopicName();
			String topicSubject = t.getSubject().subjectName;
			String topicLecturer = t.getLecturer().lecturerId;
			Integer topTopic = t.getTopTopic();
			String topicNote = t.getTopicNote();
			result += (first?"":",");
			first = false;
			result += "{";
			result += "\"topicID\"" + ":\"" + topicID + "\",";
			result += "\"topicName\"" + ":\"" + topicName + "\",";
			result += "\"topicSubject\"" + ":\"" + topicSubject + "\",";
			result += "\"topicLecturer\"" + ":\"" + topicLecturer + "\",";
			result += "\"topTopic\"" + ":\"" + topTopic + "\",";
			result += "\"topicNote\"" + ":\"" + topicNote+ "\"";
			result += "}";
		}
		result += "]}";
		session.getTransaction().commit();
		return result;
	}
	
	
	@GET
	@Path("/create/{subjectID}/{lecturerID}/{topTopic}/{topicName}/{topicNote}/")
	@Produces(MediaType.TEXT_PLAIN)
	public String createSubject(@PathParam("subjectID") String subjectID,
			@PathParam("lecturerID") String lecturerID, @PathParam("topTopic") String topTopic,@PathParam("topicName") String topicName,@PathParam("topicNote") String topicNote)
	{
		Session session = SessionFactoryUtil.getSessionFactory().getCurrentSession();
		 session.beginTransaction();
		 try {
			 	Topic t = new Topic();
				t.setSubject(Subject.getSubjectByID(subjectID, session));
				t.setLecturer(Lecturer.getLecturerById(lecturerID, session));
				if(!topTopic.equals("0"))
				{
					t.setTopTopic(Integer.valueOf(topTopic));
				}
				t.setTopicName(topicName);
				t.setTopicNote(topicNote);
				session.save(t);
				session.getTransaction().commit();
		} catch (Exception e) {
			// TODO: handle exception
			session.getTransaction().rollback();
			return "error: " + e.getMessage();
		}
		 return "success";
	}
	
	@GET
	@Path("/update/{topicID}/{subjectID}/{lecturerID}/{topTopic}/{topicName}/{topicNote}/")
	@Produces(MediaType.TEXT_PLAIN)
	public String updateTopic(@PathParam("topicID") String topicID, @PathParam("subjectID") String subjectID,
			@PathParam("lecturerID") String lecturerID, @PathParam("topTopic") String topTopic,@PathParam("topicName") String topicName,@PathParam("topicNote") String topicNote)
	{
		Session session = SessionFactoryUtil.getSessionFactory().getCurrentSession();
		 session.beginTransaction();
		 try {
			 	Topic t = Topic.getTopicByID(Integer.valueOf(topicID), session);
				t.setSubject(Subject.getSubjectByID(subjectID, session));
				t.setLecturer(Lecturer.getLecturerById(lecturerID, session));
				if(!topTopic.equals("0"))
				{
					t.setTopTopic(Integer.valueOf(topTopic));
				}
				t.setTopicName(topicName);
				t.setTopicNote(topicNote);
				session.saveOrUpdate(t);
				session.getTransaction().commit();
		} catch (Exception e) {
			// TODO: handle exception
			session.getTransaction().rollback();
			return "error: " + e.getMessage();
		}
		 return "success";
	}
	
	@GET
	@Path("/delete/{topicID}")
	@Produces(MediaType.TEXT_PLAIN)
	public String deleteTopic(@PathParam("topicID") String topicID)
	{
		Session session = SessionFactoryUtil.getSessionFactory().getCurrentSession();
		 session.beginTransaction();
		 try {
			 session.delete(Topic.getTopicByID(Integer.valueOf(topicID), session));
			 session.getTransaction().commit();
		} catch (Exception e) {
			// TODO: handle exception
			session.getTransaction().rollback();
			return "error: " + e.getMessage();
		}
		 return "success";
	}
}
